package service

import (
	"context"

	"gitee.com/thewalkers/refactoring-code/model"
	"go.uber.org/zap"
)

// DirectMethodService acts as a struct for injecting an implementation of EmployeeRepository
// for use in service methods
type DirectMethodService struct {
	DirectMethodRepository  model.DirectMethodRepository
	PaymentMethodRepository model.PaymentMethodRepository
}

// DMConfig will hold repository that will eventually be injected into this
// service layer
type DMConfig struct {
	DirectMethodRepository  model.DirectMethodRepository
	PaymentMethodRepository model.PaymentMethodRepository
}

// NewMailMethodService is a factory function for
// initializing a EmployeeService with its repository layer dependencies
func NewDirectMethodService(d *DMConfig) model.DirectMethodService {
	return &DirectMethodService{
		DirectMethodRepository:  d.DirectMethodRepository,
		PaymentMethodRepository: d.PaymentMethodRepository,
	}
}

func (d *DirectMethodService) Create(ctx context.Context, id int64, direct *model.DirectMethod) error {
	paymentMethod, err := d.PaymentMethodRepository.FindByEmlpoyeeID(ctx, id)
	if err != nil {
		zap.L().Error("service can not find payment method", zap.Error(err))
		return err
	}

	direct.PaymentMethodID = paymentMethod.ID
	err = d.DirectMethodRepository.Create(ctx, direct)

	return err
}

func (d *DirectMethodService) Update(ctx context.Context, id int64, direct *model.DirectMethod) error {
	paymentMethod, err := d.PaymentMethodRepository.FindByEmlpoyeeID(ctx, id)
	if err != nil {
		zap.L().Error("service can not find payment method", zap.Error(err))
		return err
	}

	direct.PaymentMethodID = paymentMethod.ID
	err = d.DirectMethodRepository.Update(ctx, direct)

	return err
}
